package com.ctrip.demo.tree;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UserTest {

    static Logger logger = LoggerFactory.getLogger(UserTest.class);


    static List<User> userList = new ArrayList<>();
    public static void main(String[] args) {
        logger.trace("start startstartstartstartstartstartstartstartstartstartstartstart");
        User user1 = new User("战三1",null,1);
        User user2 = new User("战三2",1,2);
        User user3 = new User("战三3",2,3);
        User user4 = new User("战三4",1,4);
        User user5 = new User("战三5",null,5);
        User user6 = new User("战三6",null,6);
        User user7 = new User("战三7",null,7);
//        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        userList.add(user4);
        userList.add(user5);
        userList.add(user6);
        userList.add(user7);

        UserIndo userIndo = new UserIndo(user1.getName(), user1.getParentId(), user1.getUserId());

        List<UserIndo> users = builTree(userIndo);
        System.out.println(JSONObject.toJSON(users));


//        String regEx = "@\\[\\[(.*?)]]";
//        String s = "@[[{\"displayname\":\"Yaping Hong (洪亚萍)\",\"corpName\":\"Trip\",\"corpId\":\"ctrip\",\"userId\":\"S74509\",\"imid\":\"openim1535950175697vpsgcyirgef05\",\"c_name\":\"Yaping Hong (洪亚萍)\",\"sn\":\"Yaping Hong (洪亚萍)\"}]]@[[{\"displayname\":\"Yongkun Huang (黄永琨)\",\"corpName\":\"Trip\",\"corpId\":\"ctrip\",\"userId\":\"TR011018\",\"imid\":\"openim1622691546642ejbqkebsowx07\",\"c_name\":\"Yongkun Huang (黄永琨)\",\"sn\":\"Yongkun Huang (黄永琨)\"}]]test";
//        Pattern pat = Pattern.compile(regEx);
//        Matcher mat = pat.matcher(s);
//        System.out.println();
//        boolean result = mat.find();
//        while (result){
////            System.out.println(mat.group(0));
////            System.out.println(mat.group(1));
//
//            MentionAccountInfo mentionAccountInfo = JSONObject.parseObject(mat.group(1), MentionAccountInfo.class);
//
//            System.out.println(mentionAccountInfo);
//
//            s = s.replace(mat.group(0), "@" + mentionAccountInfo.getDisplayname());
//            System.out.println(s);
//            result = mat.find();
//
//
//
//        }

//        System.out.println(Integer.bitCount(6));
    }



    //建立树形结构
    public static List<UserIndo> builTree(UserIndo userIndo) {
        List<UserIndo> treeMenus = new ArrayList<UserIndo>();
//        for (User menuNode : getRootNode()) {
        userIndo = buildChilTree(userIndo);
            treeMenus.add(userIndo);
//        }
        return treeMenus;
    }

    //递归，建立子树形结构
    private static UserIndo buildChilTree(UserIndo userIndo) {
        List<UserIndo> chilMenus = new ArrayList<UserIndo>();
        for (User menuNode : userList) {
            if (userIndo.getUserId().equals(menuNode.getParentId())) {
                chilMenus.add(buildChilTree(userToUserIndo(menuNode)));
            }
        }
        userIndo.setChildren(chilMenus);
        return userIndo;
    }

    private static UserIndo userToUserIndo(User user) {
        return new UserIndo(user.getName(), user.getParentId(), user.getUserId());
    }

    //获取根节点
    private static List<User> getRootNode() {
        List<User> rootMenuLists = new ArrayList<User>();
        for (User menuNode : userList) {
            if (menuNode.getParentId() == null) {
                rootMenuLists.add(menuNode);
            }
        }
        return rootMenuLists;
    }
}
